<?php

include_once($templatepath."BoardEntry.class.php");

/**
 * Represents a thread in the board
 *
 * @author Marcel Linnenfelser
 */
class BoardThread extends BoardEntry
{

    var $childrenByParent;

    /**
     * Constructor
     *
     * @access public
     * @param  array   data  Thread entries
     */
    function BoardThread($data, $entryData, & $threadTemplate, & $entryTemplate)
    {
        // Tiefe des Topentry auf null setzen
        $this->depth = 0;
        $this->template = & $threadTemplate;
        $this->data = $data;
        if (is_array($entryData))
        {
            for ($i = 0; $i < count($entryData); $i ++)
            {
                if ($entryData[$i]["parent_id"] != 0)
                {
                    $this->childrenByParent[$entryData[$i]["parent_id"]][] = 
                        new BoardEntry($entryData[$i], $entryTemplate);
                }
                else
                {
                    $this->add(new BoardEntry($entryData[$i], $entryTemplate));
                }
            }
            for ($i = 0; $i < count($this->children); $i++)
            {
                $this->addChildren($this->children[$i]);
            }
        }
    }

    /**
     * Adds the children to the entry given as parameter and calls itself for the 
     * children of the entry
     *
     * @access protected
     * @param  BoardEntry  entry  The entry which children have to be added
     */
    function addChildren(& $entry)
    {
        // Add the children for this entry if there are some
        if (is_array($this->childrenByParent[$entry->getID()]))
        {
            $children = & $this->childrenByParent[$entry->getID()];
            $entry->add($children);
            // call this method for the children
            for ($i = 0; $i < count($children); $i ++)
            {
                $this->addChildren($children[$i]);
            }
        }
    }
    
    function paint($mode)
    {
        $this->template->paint($mode, $this->children, $this->data);
    }

}
?>